package com.s7.recycler.helper;


import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.s7.recycler.adapter.BaseRecyclerAdapter;
import com.s7.recycler.listener.Recycler;

import java.util.List;

/**
 * Created by Administor on 2018\7\6 0006.
 * RecyclerView普通工具--无功能
 * 兼容Grid布局、Linear布局、StaggeredGrid布局（支持布局切换）
 */
public class RecyclerHelper<T> {

    /**
     * adapter 存在的标志
     */
    private boolean hasInitAdapter = false;

    private BaseRecyclerAdapter<T> mAdapter;
    private RecyclerView mRecycler;
    private RecyclerView.LayoutManager mManager;
    private RecyclerView.ItemDecoration mDecoration;
    private Recycler.OnItemClickListener<T> mItemClick;
    private Recycler.OnCheckedChangeListener<T> mCheckedChange;
    private Recycler.OnClickListener<T> mClick;
    private Recycler.OnItemLongClickListener<T> mItemLongClick;

    /**
     * 设置Adapter
     * @param mAdapter
     */
    protected RecyclerHelper setAdapter(BaseRecyclerAdapter<T> mAdapter) {
        this.mAdapter = mAdapter;
        return this;
    }

    /**
     * 设置RecyclerView
     * @param mRecycler
     */
    protected RecyclerHelper setRecycler(RecyclerView mRecycler) {
        this.mRecycler = mRecycler;
        return this;
    }

    /**
     * 设置布局方式
     * @param mManager
     * @return
     */
    public RecyclerHelper setManager(RecyclerView.LayoutManager mManager) {
        this.mManager = mManager;
        return this;
    }

    /**
     * 设置分割线
     * @param mDecoration
     * @return
     */
    public RecyclerHelper setDecoration(RecyclerView.ItemDecoration mDecoration) {
        this.mDecoration = mDecoration;
        return this;
    }

    public RecyclerHelper setCheckedChange(Recycler.OnCheckedChangeListener<T> mCheckedChange) {
        this.mCheckedChange = mCheckedChange;
        return this;
    }

    protected RecyclerHelper setClick(Recycler.OnClickListener<T> mClick) {
        this.mClick = mClick;
        return this;
    }

    protected RecyclerHelper setItemClick(Recycler.OnItemClickListener<T> mItemClick) {
        this.mItemClick = mItemClick;
        return this;
    }

    protected RecyclerHelper setItemLongClick(Recycler.OnItemLongClickListener<T> mItemLongClick) {
        this.mItemLongClick = mItemLongClick;
        return this;
    }

    protected void initAdapter(){

        if (mAdapter == null || mRecycler == null || mManager == null) {
            return;
        }

        onManager();

        if (mDecoration != null) {
            mRecycler.addItemDecoration(mDecoration);
        }

        mAdapter.setCheckedChange(mCheckedChange)
                .setClick(mClick)
                .setItemClick(mItemClick)
                .setItemLongClick(mItemLongClick);
        mRecycler.setAdapter(mAdapter);
        hasInitAdapter = true;
    }

    /**
     * 瀑布流布局管理器处理
     */
    private void onManager() {
        if (mManager instanceof StaggeredGridLayoutManager) {
            ((StaggeredGridLayoutManager) mManager).setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        }
        mRecycler.setLayoutManager(mManager);
        mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (mManager instanceof StaggeredGridLayoutManager) {
                    //防止第一行到顶部有空白区域
                    ((StaggeredGridLayoutManager) mManager).invalidateSpanAssignments();
                }
            }
        });
    }

    /**
     * 初始化
     */
    public void init() {
        if(!hasInitAdapter){
            initAdapter();
        }
    }

    /**
     * 切换RecyclerView 布局管理
     * @param layoutManager
     */
    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        if (mRecycler != null && mAdapter != null && layoutManager != null) {
            onManager();
            notifyAdapter();
        }
    }

    /**
     * 刷新Adapter
     * @param datas
     */
    public void notifyAdapter(List<T> datas) {
        init();
        mAdapter.onRefresh(datas);
    }

    /**
     * 刷新Adapter
     */
    public void notifyAdapter() {
        init();
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 替换指定位置数据
     * @param position
     * @param data
     */
    public void notifyReplace(int position, T data) {
        mAdapter.replace(position, data);
    }

    /**
     * 删除指定位置的内容
     * @param position
     */
    public void notifyRemove(int position) {
        mAdapter.remove(position);
    }

    /**
     * 获取所有数据
     * @return
     */
    public List<T> getData() {
        return mAdapter.getDatas();
    }

    public static class Builder<T> {
        private BaseRecyclerAdapter<T> mAdapter;
        private RecyclerView mRecycler;
        private RecyclerView.LayoutManager mManager;
        private RecyclerView.ItemDecoration mDecoration;
        private Recycler.OnCheckedChangeListener<T> mCheckedChange;
        private Recycler.OnClickListener<T> mClick;
        private Recycler.OnItemClickListener<T> mItemClick;
        private Recycler.OnItemLongClickListener<T> mItemLongClick;

        public Builder setRecycler(RecyclerView mRecycler, final RecyclerView.LayoutManager mManager) {
            return setRecycler(mRecycler, mManager, null);
        }

        public Builder setRecycler(RecyclerView mRecycler, final RecyclerView.LayoutManager mManager,
                                   RecyclerView.ItemDecoration mDecoration) {
            this.mRecycler = mRecycler;
            this.mManager = mManager;
            this.mDecoration = mDecoration;
            return this;
        }

        public Builder setAdapter(BaseRecyclerAdapter<T> mAdapter) {
            this.mAdapter = mAdapter;
            return this;
        }

         public Builder setCheckedChange(Recycler.OnCheckedChangeListener<T> mCheckedChange) {
            this.mCheckedChange = mCheckedChange;
            return this;
        }

        public Builder setClick(Recycler.OnClickListener<T> mClick) {
            this.mClick = mClick;
            return this;
        }

        public Builder setItemClick(Recycler.OnItemClickListener<T> mItemClick) {
            this.mItemClick = mItemClick;
            return this;
        }

        public Builder setItemLongClick(Recycler.OnItemLongClickListener<T> mItemLongClick) {
            this.mItemLongClick = mItemLongClick;
            return this;
        }

        public <T> RecyclerHelper<T> build() {
            RecyclerHelper mHelper = new RecyclerHelper<T>()
                    .setRecycler(mRecycler)
                    .setManager(mManager)
                    .setDecoration(mDecoration)
                    .setAdapter(mAdapter)
                    .setItemClick(mItemClick)
                    .setCheckedChange(mCheckedChange)
                    .setClick(mClick)
                    .setItemClick(mItemClick)
                    .setItemLongClick(mItemLongClick);
            mHelper.initAdapter();
            return mHelper;
        }
    }
}
